export const validatString = count => (rule, value, callback) => {
    value = (value || '').toString();

    if (value.length >= 0 && value.length <= count) {
        callback();
    } else {
        return callback(new Error(`长度限制为${count}个字符`));
    }
};

export default function getRules() {
    const RULES = {
        name: [
            {required: true, message: '账户名称必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        type: [
            {required: true, message: '类型必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        remark: [{validator: validatString(200), trigger: ['change', 'blur']}]
    };

    return RULES;
}
export async function formValidates(values, refs) {
    const $refs = refs || this.$refs;
    const handers = [];
    values.forEach(item => {
        const uuid = item.uuid;
        const formNames = [`form_name_${uuid}`, `form_type_${uuid}`, `form_remark_${uuid}`];
        formNames.forEach(formName => {
            if (!$refs[formName]) {
                console.warn('formName', formName);
                return;
            }
            const hander = new Promise((resolve, reject) => {
                $refs[formName].validate((valid, fields) => {
                    if (valid) {
                        resolve(valid);
                        return;
                    }
                    reject(valid);
                });
            });
            handers.push(hander);
        });
    });
    await Promise.all(handers);
}
